package chat.chit.tit.mit;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import chat.chit.tit.mit.variables.Variables;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {

	private EditText etUsername;
	private EditText etPassword;
	private EditText etEmail;
	private EditText etFullname;

	public static final String HOST = "192.168.100.27";
	public static final int PORT = 5222;
	public static final String ADMIN_USER = "trangtth";
	public static final String ADMIN_PASS = "123456";

	private String username;
	private String email;
	private String password;
	private String fullname;
	private XMPPConnection connection;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		postInit();
	}

	public void registerAction(View v) {
		username = etUsername.getText().toString();
		password = etPassword.getText().toString();
		email = etEmail.getText().toString();
		fullname = etFullname.getText().toString();
		if (!LoginActivity.isEmpty(username)
				&& !LoginActivity.isEmpty(password)) {
			connect();
		}
	}

	public void postInit() {
		etUsername = (EditText) findViewById(R.id.et_username);
		etPassword = (EditText) findViewById(R.id.et_password);
		etEmail = (EditText) findViewById(R.id.et_email);
		etFullname = (EditText) findViewById(R.id.et_name);
	}

	public void createUser(XMPPConnection connection) {

		try {
//			connection.connect();
			AccountManager am = new AccountManager(connection);
			Map<String, String> mp = new HashMap<String, String>();
			mp.put("username", username);
			mp.put("password", password);
			mp.put("email", email);
			mp.put("name", fullname);
			am.createAccount(username, password, mp);
			connection.login(username, password);
			Variables.connection = connection;
			Intent in = new Intent(RegisterActivity.this, MainActivity.class);
			startActivity(in);
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this, "Lỗi đăng kí không thành công!",
					Toast.LENGTH_LONG).show();
		}
	}
	
	public void connect() {

		final ProgressDialog dialog = ProgressDialog.show(this,
				"Connecting...", "Please wait...", false);

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// Create a connection
				ConnectionConfiguration connConfig = new ConnectionConfiguration(
						HOST, PORT);
				connConfig.setSASLAuthenticationEnabled(false);
				connConfig.setSecurityMode(SecurityMode.disabled);
				XMPPConnection connection = new XMPPConnection(connConfig);

				try {
					connection.connect();
					Log.i("XMPPChatDemoActivity",
							"Connected to " + connection.getHost());
				} catch (XMPPException ex) {
					Log.e("XMPPChatDemoActivity", "Failed to connect to "
							+ connection.getHost());
					Log.e("XMPPChatDemoActivity", ex.toString());
				}
				
				createUser(connection);
				dialog.dismiss();
			}
		});
		t.start();
		dialog.show();
	}

}